package cn.ds.assets.module.assets.service.dataQualityRuleSet;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.dataQualityRuleSet.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.dataQualityRuleSet.BaseDataQualityRuleSetDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.dataQualityRuleSet.BaseDataQualityRuleSetConvert;
import cn.ds.assets.module.assets.dal.mysql.dataQualityRuleSet.BaseDataQualityRuleSetMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 数据质量规则设置 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseDataQualityRuleSetServiceImpl implements BaseDataQualityRuleSetService {

    @Resource
    private BaseDataQualityRuleSetMapper baseDataQualityRuleSetMapper;

    @Override
    public Long createBaseDataQualityRuleSet(BaseDataQualityRuleSetCreateReqVO createReqVO) {
        // 插入
        BaseDataQualityRuleSetDO baseDataQualityRuleSet = BaseDataQualityRuleSetConvert.INSTANCE.convert(createReqVO);
        baseDataQualityRuleSetMapper.insert(baseDataQualityRuleSet);
        // 返回
        return baseDataQualityRuleSet.getId();
    }

    @Override
    public void updateBaseDataQualityRuleSet(BaseDataQualityRuleSetUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseDataQualityRuleSetExists(updateReqVO.getId());
        // 更新
        BaseDataQualityRuleSetDO updateObj = BaseDataQualityRuleSetConvert.INSTANCE.convert(updateReqVO);
        baseDataQualityRuleSetMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseDataQualityRuleSet(Long id) {
        // 校验是否存在
        validateBaseDataQualityRuleSetExists(id);
        // 逻辑删除
        baseDataQualityRuleSetMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseDataQualityRuleSetExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseDataQualityRuleSetDO dataDo = baseDataQualityRuleSetMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseDataQualityRuleSetDO getBaseDataQualityRuleSet(Long id) {
        return baseDataQualityRuleSetMapper.selectById(id);
    }

    @Override
    public List<BaseDataQualityRuleSetDO> getBaseDataQualityRuleSetList(Collection<Long> ids) {
        return baseDataQualityRuleSetMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseDataQualityRuleSetDO> getBaseDataQualityRuleSetPage(BaseDataQualityRuleSetPageReqVO pageReqVO) {
        return baseDataQualityRuleSetMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseDataQualityRuleSetDO> getBaseDataQualityRuleSetList(BaseDataQualityRuleSetExportReqVO exportReqVO) {
        return baseDataQualityRuleSetMapper.selectList(exportReqVO);
    }

}
